// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "NetworkGameSession.h"
#include "Framework/BaseGameInstance.h"
#include "Subsystem/ProtocolSubsystem.h"
#include "NetworkGameInstance.generated.h"

// TODO: 好友系统、排行榜系统、商店系统、成就系统
UCLASS()
class MISCELLANEOUSPROJECT_API UNetworkGameInstance : public UBaseGameInstance
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable,Category = "Network")
	void ParseVersion(TArray<FString> content, FString SavePath);
	UFUNCTION(BlueprintCallable,Category = "Network")
	FString ParseURL(FString content);
	UFUNCTION(BlueprintCallable,Category = "Network")
	TArray<FString> ParsePak(FString content);

	
public:
	UFUNCTION(BlueprintCallable,Category = "Network")
	void HostGame();

	UFUNCTION(BlueprintCallable,Category = "Network")
	void FindSessions();

	UFUNCTION(BlueprintCallable,Category = "Network")
	void JoinSessions();
protected:
	virtual void OnCreatePresenceSessionComplete(FName SessionName, bool bSuccess);
	virtual void Init() override;
private:
	void MountedAllPaks(TEnumAsByte<DownloadResult> Result);
	ANetworkGameSession* GetGameSession();
	bool HostGame(ULocalPlayer* LocalPlayer, const FString& GameType, const FString& InTravelURL);
	bool JoinSession(ULocalPlayer* LocalPlayer, int32 SessionIndexInSearchResults);
	bool JoinSession(ULocalPlayer* LocalPlayer, const FOnlineSessionSearchResult& SearchResult);
	bool FindSessions(ULocalPlayer* PlayerOwner, bool bIsDedicatedServer, bool bFindLAN);

	void FinishSessionCreation(EOnJoinSessionCompleteResult::Type Result);
	void FinishJoinSession(EOnJoinSessionCompleteResult::Type Result);
	void InternalTravelToSession(const FName& SessionName);
	void OnJoinSessionComplete(EOnJoinSessionCompleteResult::Type Result);
	void OnSearchSessionsComplete(bool bWasSuccessful);

	void DebugMessage(FString Message);

	FString TravelURL;
	FString MapPath;
	FString MapLeftPath;
	FDelegateHandle OnCreatePresenceSessionCompleteDelegateHandle;
	FDelegateHandle OnJoinSessionCompleteDelegateHandle;
	FDelegateHandle OnSearchSessionsCompleteDelegateHandle;
	class UGameVersionSubsystem* GameVersionSubsystem;
};
